package com.touchfish.material.common;

import cn.hutool.core.util.ObjectUtil;
import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;

/**
 * 通用返回
 * @author touchfish
 * @date 2023/2/16
 */
@Data
public class BaseResult extends HashMap<String, Object> implements Serializable {
    private static final long serialVersionUID = 1L;
    public static final String CODE = "code";
    public static final String MSG = "msg";
    public static final String DATA = "data";
    public static final String TOTAL = "total";
    public static final String SUCCESS_MSG = "操作成功";
    public static final String ERROR_MSG = "操作失败";

    public BaseResult() {
    }

    public BaseResult(int code, String msg) {
        super.put(CODE, code);
        super.put(MSG, msg);
    }

    public BaseResult(int code, String msg, Object data, int total) {
        super.put(CODE, code);
        super.put(MSG, msg);
        if (ObjectUtil.isNotEmpty(data)) {
            super.put(DATA, data);
        }
        if (ObjectUtil.isNotEmpty(total)) {
            super.put(TOTAL, total);
        }

    }

    public BaseResult(int code, String msg, Object data) {
        super.put(CODE, code);
        super.put(MSG, msg);
        if (ObjectUtil.isNotEmpty(data)) {
            super.put(DATA, data);
        }

    }

    public static BaseResult success() {
        return success(SUCCESS_MSG);
    }

    public static BaseResult success(Object data) {
        return success(SUCCESS_MSG, data);
    }

    public static BaseResult success(String msg) {
        return success(msg, (Object) null);
    }

    public static BaseResult success(String msg, Object data) {
        return new BaseResult(200, msg, data);
    }

    public static BaseResult success(String msg, Object data, int total) {
        return new BaseResult(200, msg, data, total);
    }

    public static BaseResult success(Object data, int total) {
        return new BaseResult(200, SUCCESS_MSG, data, total);
    }


    public static BaseResult error() {
        return error(ERROR_MSG);
    }

    public static BaseResult error(String msg) {
        return error(msg, (String) null);
    }

    public static BaseResult error(String msg, String data) {
        return new BaseResult(500, msg, data);
    }
}
